#ifndef ROPlayer_H
#define ROPlayer_H

#include <QObject>

#include <QtQmlIntegration>

#include "QQuickROChannel.h"

//共享对象系统中的玩家对象，一个玩家对象相当于一个连接到服务端的Player
//只有服务端会存在Player对象
//Player对象，Channel对象共轭，任何一个被删除都会导致对方被删除，从而导致客户端被踢出

class QQuickROChannel;
class ROHost;

class ROPlayer:public QObject
{
    Q_OBJECT
    QML_NAMED_ELEMENT(ROPlayer)
public:
    enum DisconnectType
    {
        LostConnection,//服务端和客户端直接断开连接，不正常退出
        Logout,//客户端主动要求退出
        Kicked//服务端主动踢出客户端
    };
    Q_ENUM(DisconnectType);
public:
    ROPlayer();

    bool isHostPlayer();//该Player是否属于房主

    int playerId() const;

    Q_INVOKABLE void kick(QString message = "");//将客户端踢出,对房主无法使用该方法

signals:
    void playerIdChanged();
    void disconnected(DisconnectType type,QString message);//用户退出登录了
private:
    void setPlayerId(int newPlayerId);
    int m_playerId = 0;//玩家的id
    QQuickROChannel* channel = nullptr;//与对应客户端的连接通道,监听服务器的player的channel为null
    ROHost* m_host = nullptr;

    Q_PROPERTY(int playerId READ playerId NOTIFY playerIdChanged FINAL)
    Q_PROPERTY(bool isHostPlayer READ isHostPlayer CONSTANT)
    friend class ROHost;
};


#endif
